// Package avatar -----------------------------
// @file      : avatarAllRoomModule.go
// @author    : WuZhouHai
// @contact   : wzh200x@126.com
// @time      : 2024/7/17 18:19
// @desc      :
// -------------------------------------------
package avatar

import (
	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/errors/gcode"
	"github.com/gogf/gf/v2/errors/gerror"
	"phoenix/apps/examples/phoenix_app_dwys/defines"
	"phoenix/apps/examples/phoenix_app_dwys/defines/entityType"
	"phoenix/apps/examples/phoenix_app_dwys/model/entityModule/avatar"
	"phoenix/phoenix/defines/dataType"
	"phoenix/phoenix/entitys/entityModule"
	"phoenix/phoenix/interfaces"
)

type AvatarAllRoomModule struct {
	entityModule.EntityModuleBase
	roomInfos *gmap.IntIntMap
}

func (this *AvatarAllRoomModule) Init() {
	this.EntityModuleBase.Init()
	this.SyncRoomInfos()
}

func (this *AvatarAllRoomModule) SyncRoomInfos() {
	this.roomInfos.Clear()
	datas := this.Model.GetModelDatas()
	datas.Iterator(func(key int, value interface{}) bool {
		if data, ok := value.(*avatar.AvatarAllRoomModelData); ok {
			this.roomInfos.Set(int(data.RoomId), data.Dbid)
		}
		return true
	})
}

func (this *AvatarAllRoomModule) GetAllRoomInfo() *gmap.IntAnyMap {
	return this.Model.GetModelDatas()
}

func (this *AvatarAllRoomModule) GetRoom(roomId int32) *avatar.AvatarAllRoomModelData {
	if !this.roomInfos.Contains(int(roomId)) {
		return nil
	}
	dbid := this.roomInfos.Get(int(roomId))
	return this.Model.GetModelData(dbid).(*avatar.AvatarAllRoomModelData)
}

func (this *AvatarAllRoomModule) CheckRoomIsActive(roomId int32) bool {
	datas := this.GetRoom(roomId)
	if datas != nil {
		return datas.Active == 1
	}
	return false
}

func (this *AvatarAllRoomModule) ActiveRoom(roomId int32) error {
	if this.CheckRoomIsActive(roomId) {
		return nil
	}
	datas := this.GetRoom(roomId)
	if datas != nil {
		datas.Active = 1
		this.Model.SetModelDataUpdate(datas.Dbid)
		return nil
	}
	return gerror.NewCodef(gcode.New(defines.CUSTOMER_ERROR_ROOM_NOT_EXIST, "", nil), "店铺不存在")
}

func NewAvatarAllRoomModule(entityDBID dataType.PhoenixTypeDataBaseId) interfaces.IEntityModule {
	return &AvatarAllRoomModule{
		EntityModuleBase: entityModule.NewEntityModuleBase(entityType.EntityName_Avatar,
			defines.ModuleName_Avatar_AllRoom, entityDBID),
		roomInfos: gmap.NewIntIntMap(true),
	}
}
